<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:h="http://java.sun.com/jsf/html"
      xmlns:ui="http://java.sun.com/jsf/facelets"
      xmlns:p="http://primefaces.org/ui"
      xmlns:f="http://java.sun.com/jsf/core">

    <ui:composition template="layout.xhtml">
        <ui:define name="body">

            <h:form id="cepForm" enctype="multipart/form-data">
                <h1 class="title">Escrituras</h1>
                <p:growl id="messages" showDetail="true" autoUpdate="true"  /> 
                <div class="row">
                    <div class="twelve columns"> 
                        <fieldset>
                            <legend> <h4 style="margin:0 0 10px 0">Cadastro</h4></legend>
                            <p:panel id="charterPanel" style="background: none; border: none;" 
                                     styleClass="container_24 clearfix ">


                                <p:outputLabel value="Número: " styleClass="grid_3"/>
                                <p:inputText styleClass="grid_8" value="#{managerCep.charter.esc}"/>

                                <div class="clear"/>

                                <p:outputLabel value="Tipo Ato: " for="idato" styleClass="grid_3"/>
                                <p:inputText styleClass="grid_7" value="#{managerCep.actCep.description}"
                                             readonly="true"/>
                                <p:commandButton  icon="ui-icon-plus" process="@this" 
                                                  styleClass="ui-button-icon-only grid_2" 
                                                  actionListener="#{managerCep.loadActCep()}"
                                                  update="@this, :cepForm:actCepPanelSearch"
                                                  style="width: 25px; height: 28px; margin-left:0px;" 
                                                  onclick="dlgActCepDialog.show();"/>
                                
                                <p:autoComplete styleClass="grid_8" style="width: 28.44%" dropdown="true" converter="actcepconverter"   
                                                var="actCepValue" itemLabel="#{actCepValue.description}" rendered="false"
                                                itemValue="#{actCepValue}" value="#{managerCep.actCep}"
                                                scrollHeight="200" forceSelection="true"
                                                completeMethod="#{managerActCep.autocomplete}"
                                                id="idato"/>  
                                

                                <p:outputLabel value="Natureza: " styleClass="grid_4"/>
                                <p:inputText styleClass="grid_7" value="#{managerCep.natureWriting.description}"
                                             readonly="true"/>
                                <p:commandButton  icon="ui-icon-plus" process="@this" 
                                                  styleClass="ui-button-icon-only grid_2" 
                                                  actionListener="#{managerCep.loadNature()}"
                                                  update="@this, :cepForm:naturePanelSearch"
                                                  style="width: 25px; height: 28px; margin-left:0px;" 
                                                  onclick="dlgNatureDialog.show();"/>
                                
                                <p:autoComplete styleClass="grid_8" style="width: 28.44%" dropdown="true" converter="naturewritingconverter"   
                                                var="natureValue" itemLabel="#{natureValue.description}"  rendered="false"
                                                itemValue="#{natureValue}" value="#{managerCep.natureWriting}"
                                                scrollHeight="200" forceSelection="true"
                                                completeMethod="#{managerNatureWriting.autocomplete}" />

                                

                                <div class="clear"/>
                                <div class="clear"/>
                                <p:outputLabel value="Data Ato: " styleClass="grid_3"/>
                                <p:calendar showOn="button" styleClass="grid_8" style="width:32.44%"
                                            pattern="dd/MM/yyyy"
                                            value="#{managerCep.charter.dateAct}"/>

                                <p:outputLabel value="Valor: " styleClass="grid_4"/>
                                <p:inputText styleClass="grid_8" value="#{managerCep.charter.currency}"
                                             onkeypress="jQuery(this).autoNumeric({aSign: 'R$ ', aSep: '.', aDec: ','} );">
                                    <f:convertNumber type="currency"  locale="pt,BR"  />
                                </p:inputText>


                                <div class="clear"/>

                                <p:outputLabel value="Livro: " styleClass="grid_3" />
                                <p:inputMask mask="?999999999" placeHolder="" styleClass="grid_8" value="#{managerCep.charter.book}"/>

                                <p:outputLabel value="Livro Compl: " styleClass="grid_4"/>
                                <p:inputText styleClass="grid_8" value="#{managerCep.charter.bookComp}"/> 

                                <div class="clear"/>

                                <p:outputLabel value="Folha: " styleClass="grid_3" />
                                <p:inputMask mask="?999999999" placeHolder="" styleClass="grid_8" value="#{managerCep.charter.leaf}"/> 

                                <p:outputLabel value="Folha Compl: " styleClass="grid_4"/>
                                <p:inputText styleClass="grid_8" value="#{managerCep.charter.leafComp}"/> 

                                <div class="clear"/>

                                <p:outputLabel value="Cidade: " styleClass="grid_3"/>
                                <p:autoComplete styleClass="grid_8" style="width: 28.44%" dropdown="true" converter="cityconverter"   
                                                var="cityValue" itemLabel="#{cityValue.name.toString().concat(' - ')}#{cityValue.country.slg}" 
                                                itemValue="#{cityValue}" value="#{managerCep.city}"
                                                scrollHeight="200" forceSelection="true"
                                                completeMethod="#{managerActCep.autocompleteCity}" />  

                                <div class="clear"/>
                                <p:outputLabel value="Objeto: " styleClass="grid_3"/>
                                <p:inputTextarea cols="100" rows="4" maxlength="1000"
                                                 styleClass="grid_20" value="#{managerCep.charter.description}"/> 
                            </p:panel>

                        </fieldset>
                        <fieldset>
                            <legend> <h4 style="margin:0 0 10px 0">Partes</h4></legend>

                            <p:panel id="partsPanel" style="background: none; border: none;" styleClass="container_24 clearfix ">

                                <p:outputLabel value="Pessoa: "  styleClass="grid_3"/>
                                <p:selectOneRadio id="typePerson" style="margin-top:-8px;" styleClass="grid_8"
                                                  value="#{managerCep.people.tipoDocumento}" >  
                                    <f:selectItems value="#{managerCep.typeDocumentList()}" />
                                    <p:ajax event="change" update=":cepForm:partsPanel" 
                                            listener="#{managerCep.typeDocumentPeople}" />
                                </p:selectOneRadio>

                                <div class="clear"/>

                                <p:outputLabel id="typeDocumentLabel" 
                                               value="#{managerCep.people.tipoDocumento}: " 
                                               styleClass="grid_3"/>

                                <p:inputMask id="typeDocumentInput" 
                                             value="#{managerCep.people.tipoDocumento.type eq 'CPF' ? 
                                                      managerCep.people.cpf : managerCep.people.cnpj}"
                                             mask="#{managerCep.people.tipoDocumento.type eq 'CPF' ? 
                                                     '999.999.999-99':'99.999.999/9999-99'}"
                                             styleClass="grid_8" > 
                                    <p:ajax event="blur" update=":cepForm:partsPanel" onstart="bui.show()" oncomplete="bui.hide()"
                                            listener="#{managerCep.ajaxVerifyPeopleExist()}" />
                                </p:inputMask>

                                <p:blockUI block=":cepForm:partsPanel" widgetVar="bui"> 
                                    <p:graphicImage value="media/css/images/ajax-loader.gif"/>
                                </p:blockUI>

                                <div class="clear"/>

                                <p:outputLabel value="Nome: " styleClass="grid_3" />
                                <p:inputText styleClass="grid_20" value="#{managerCep.people.name}"/> 

                                <div class="clear"/>

                                <p:outputLabel value="RG: " rendered="#{managerCep.people.tipoDocumento.type eq 'CPF'}" styleClass="grid_3"/>
                                <p:inputText styleClass="grid_8" rendered="#{managerCep.people.tipoDocumento.type eq 'CPF'}" value="#{managerCep.people.rg}"/> 

                                <p:outputLabel value="Orgão emissor: " rendered="#{managerCep.people.tipoDocumento.type eq 'CPF'}" styleClass="grid_4"/>
                                <p:inputText styleClass="grid_8" rendered="#{managerCep.people.tipoDocumento.type eq 'CPF'}" value="#{managerCep.people.organ}"/> 

                                <div class="clear"/>

                                <p:outputLabel value="Data nasc: " rendered="#{managerCep.people.tipoDocumento.type eq 'CPF'}" styleClass="grid_3"/>
                                <p:calendar showOn="button" styleClass="grid_8" rendered="#{managerCep.people.tipoDocumento.type eq 'CPF'}" style="width:32.44%" 
                                            value="#{managerCep.people.nasc}"/> 

                                <p:outputLabel value="Qualidade: " 
                                               styleClass="#{managerCep.people.tipoDocumento.type eq 'CPF' ? 'grid_4':'grid_3'}"/>
                                <p:autoComplete styleClass="grid_8" style="width: 28.44%" dropdown="true" converter="qualityconverter"   
                                                var="qualityValue" itemLabel="#{qualityValue.description}" scrollHeight="200"
                                                itemValue="#{qualityValue}" value="#{managerCep.part.partQuality}" 
                                                completeMethod="#{managerCesdi.autocompleteQuality}" />

                                <div class="clear"/>

                                <div class="buttonAction">
                                    <p:commandButton value="Adicionar" actionListener="#{managerCep.addParts()}"
                                                     icon="ui-icon-plus"
                                                     process="@this, :cepForm:partsPanel, 
                                                     :cepForm:dataTableParts"
                                                     update=":cepForm:partsPanel, 
                                                     :cepForm:dataTableParts" styleClass="ui-priority-primary" />
                                    <p:commandButton value="Limpar"  icon="ui-icon-trash" />
                                </div>


                                <p:dataTable value="#{managerCep.parts}"
                                             emptyMessage="Nenhum registro encontrado"
                                             rowIndexVar="rowid"
                                             id="dataTableParts" var="partTable">
                                    <f:facet name="header">
                                        Partes
                                    </f:facet>
                                    <p:column headerText="Nome" width="550">
                                        #{partTable.people.name}
                                    </p:column>

                                    <p:column headerText="Documento">
                                        #{partTable.people.cpf}
                                        #{partTable.people.cnpj}
                                    </p:column>

                                    <p:column headerText="Qualidade">
                                        #{partTable.partQuality.description}
                                    </p:column>
                                    <p:column headerText="Ação">
                                        <p:commandButton  style="width: 20px; height: 20px;" 
                                                          process="@this, :cepForm:dataTableParts"
                                                          update=":cepForm:dataTableParts"
                                                          actionListener="#{managerCep.remove(rowid)}"
                                                          icon="ui-icon-trash"/>
                                    </p:column>

                                </p:dataTable>

                            </p:panel>
                        </fieldset>
                        <fieldset>
                            <legend> <h4 style="margin:0 0 10px 0">Selos</h4></legend>

                            <p:panel id="stampPanel" 
                                     style="background: none; border: none;" styleClass="container_24 clearfix ">

                                <p:outputLabel value="Série: "   styleClass="grid_3"/>
                                <p:inputText  styleClass="grid_8" value="#{managerCep.stamp.serie}"/> 

                                <p:outputLabel value="Número: "  styleClass="grid_4"/>
                                <p:inputText  styleClass="grid_8" value="#{managerCep.stamp.number}"/> 

                                <p:outputLabel value="Tipo Selo: "  styleClass="grid_3" />
                                <p:autoComplete styleClass="grid_8" style="width: 28.44%" dropdown="true"
                                                converter="typestampconverter"   forceSelection="true"
                                                value="#{managerCep.typeStampObj}"
                                                var="typeStampValue" itemLabel="#{typeStampValue.name}" 
                                                itemValue="#{typeStampValue}"
                                                completeMethod="#{managerTypeStamp.autoComplete}" >

                                    <p:column style="width:20%;text-align:center;background-color: \##{typeStampValue.color};">  
                                    </p:column>  

                                    <p:column style="width:80%">  
                                        #{typeStampValue.name} 
                                    </p:column>
                                </p:autoComplete>

                                <div class="clear"/>

                                <div class="buttonAction">
                                    <p:commandButton value="Adicionar" actionListener="#{managerCep.addStamps()}"
                                                     icon="ui-icon-plus" 
                                                     process="@this, :cepForm:stampPanel, 
                                                     :cepForm:dataTableStamps"
                                                     update=":cepForm:stampPanel, 
                                                     :cepForm:dataTableStamps" styleClass="ui-priority-primary" />
                                    <p:commandButton value="Limpar"  icon="ui-icon-trash" />
                                </div>

                                <p:dataTable id="dataTableStamps"
                                             value="#{managerCep.stamps}"
                                             var="stampTable"
                                             emptyMessage="Nenhum registro encontrado" >
                                    <f:facet name="header">
                                        Selos
                                    </f:facet>
                                    <p:column headerText="Série">
                                        #{stampTable.serie}
                                    </p:column>
                                    <p:column headerText="Número">
                                        #{stampTable.number}
                                    </p:column>
                                    <p:column headerText="Tipo">
                                        #{stampTable.typeStamp.name}
                                    </p:column>

                                </p:dataTable>
                            </p:panel>
                        </fieldset>
                        <br/>

                        <fieldset>
                            <legend> <h4 style="margin:0 0 10px 0">Anexos</h4></legend>

                            <p:fileUpload 
                                mode="advanced"  
                                label="Anexar"
                                fileUploadListener="#{managerCep.handle}"
                                multiple="true" 
                                auto="true"
                                invalidFileMessage="Tipo de arquivo não suportado."
                                invalidSizeMessage="Tamanho do arquivo não é suportado"
                                uploadLabel="Enviar"
                                cancelLabel="Cancelar"
                                immediate="true"
                                update="messages"
                                allowTypes="/(\.|\/)(gif|tif|jpe?g|png|pdf)$/"/> 

                        </fieldset>

                        <div class="buttonAction">
                            <p:commandButton value="Salvar" icon="ui-icon-disk"
                                             process="@form" update="@form"
                                             actionListener="#{managerCep.create()}" styleClass="ui-priority-primary" />
                            <p:commandButton value="Cancelar"  icon="ui-icon-trash" />
                        </div >
                    </div>
                </div>


                <p:dialog id="dlgActCepDialog"  widgetVar="dlgActCepDialog"
                          header="Selecionar Tipo de Ato" modal="true"
                          width="800"  dynamic="true" closable="false" > 

                    <p:panel id="actCepPanelSearch" style="background: none; border: none;" 
                             styleClass="container_24 clearfix ">

                        <p:dataTable id="tableActCep" var="p" value="#{managerCep.actCeps}" 
                                     styleClass="grid_24"
                                     rowIndexVar="rowid"
                                     rowKey="#{p.id}"
                                     rows="5" paginator="true"  
                                     paginatorPosition="bottom"
                                     selectionMode="single"
                                     selection="#{managerCep.actCep}"
                                     emptyMessage="Nenhum registro">  
                            <f:facet name="header">
                                Tipo de Ato
                            </f:facet>
                            <p:ajax event="rowSelect"  process="@this" update="@this"/>
                            <p:ajax event="rowUnselect"  process="@this" update="@this"/>
                            <p:column headerText="Cod">  
                                <h:outputText value="#{p.cod}"/>
                            </p:column>  
                            <p:column headerText="Nome">  
                                <h:outputText value="#{p.description}"/>
                            </p:column>
                        </p:dataTable> 

                        <div class="buttonAction">
                            <p:commandButton value="Selecionar" icon="ui-icon-search"
                                             update=":cepForm:charterPanel" 
                                             process=":cepForm:charterPanel" 
                                             immediate="false" oncomplete="dlgActCepDialog.hide();" />

                            <p:commandButton value="Remover" icon="ui-icon-trash"
                                             update=":cepForm:charterPanel" 
                                             process="@this"  actionListener="#{managerCep.initActCep()}"
                                             oncomplete="dlgActCepDialog.hide();"/>
                        </div>
                    </p:panel>
                </p:dialog>

                <p:dialog id="dlgNatureDialog"  widgetVar="dlgNatureDialog"
                          header="Selecionar Natureza do Ato" modal="true"
                          width="800"  dynamic="true" closable="false" > 

                    <p:panel id="naturePanelSearch" style="background: none; border: none;" 
                             styleClass="container_24 clearfix ">

                        <p:dataTable id="tableNature" var="n" value="#{managerCep.natureWritings}" 
                                     styleClass="grid_24"
                                     rowIndexVar="rowid"
                                     rowKey="#{n.id}"
                                     rows="5" paginator="true"  
                                     paginatorPosition="bottom"
                                     selectionMode="single"
                                     selection="#{managerCep.natureWriting}"
                                     emptyMessage="Nenhum registro">  
                            <f:facet name="header">
                                Natureza do Ato
                            </f:facet>
                            <p:ajax event="rowSelect"  process="@this" update="@this"/>
                            <p:ajax event="rowUnselect"  process="@this" update="@this"/>
                            <p:column headerText="Cod">  
                                <h:outputText value="#{n.cod}"/>
                            </p:column>  
                            <p:column headerText="Nome">  
                                <h:outputText value="#{n.description}"/>
                            </p:column>
                        </p:dataTable> 

                        <div class="buttonAction">
                            <p:commandButton value="Selecionar" icon="ui-icon-search"
                                             update=":cepForm:charterPanel" 
                                             process=":cepForm:charterPanel" 
                                             immediate="false" oncomplete="dlgNatureDialog.hide();" />

                            <p:commandButton value="Remover" icon="ui-icon-trash"
                                             update="@this, :cepForm:charterPanel" 
                                             process="@this" actionListener="#{managerCep.initNature()}"
                                             oncomplete="dlgNatureDialog.hide();" />
                        </div>
                    </p:panel>
                </p:dialog>
            </h:form>
        </ui:define>
    </ui:composition>

</html>
